//
//  EnemyBullet.cpp
//  airfight
//
//  Created by aaron more on 12-11-16.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>

#include "EnemyBulletSprite.h"


EnemyBulletSprite::EnemyBulletSprite()
:m_pType(0)
,m_pDamage(0)
,m_pFrequency(0)
,m_pSpeed(0)
,m_pDuration(0.0)
,m_pAngle(0)
,m_pGroup_num(0)
,m_pGroup_interval(0)
,m_pBegin(0)
,m_pIsUse(0)
{
    m_pFirePoint = CCPointMake(0,0);
}

bool EnemyBulletSprite::init()
{
    EntitySprite::init();
    return true;
}

EnemyBulletSprite::~EnemyBulletSprite()
{
    //CCLog("destrory enemy bullet address  = %0x , tag = %d",this,this->getTag());
}

void EnemyBulletSprite::onEnter()
{
    EntitySprite::onEnter();
    batch = (CCSpriteBatchNode*)this->getParent();
    screenSize = CCDirector::sharedDirector()->getWinSize();
    scheduleUpdate();
}

void EnemyBulletSprite::handleContact(ENTITY_TYPE type, CCDictionary *effect)
{
    
}

void EnemyBulletSprite::update(float dt)
{
    if(this->getToDestroyFlag() != 0)
    {
        this->setVisible(false);
        this->stopAllActions();
        
        b2Body *destroyBulletBody = this->getPhysicsBody();
//        b2World *w = destroyBulletBody->GetWorld();
//        if (w->IsLocked()) {
//            return;
//        }
        destroyBulletBody->SetActive(false);
        destroyBulletBody->GetWorld()->DestroyBody(destroyBulletBody);
        switch (this->getToDestroyFlag()) {
            case ENEMYBULLET_DESTROY_COLLISION:
            {
                break;
            }
            case ENEMYBULLET_DESTROY_OUT:
            {
                break;
            }
            default:
                break;
        }
        batch->removeChild(this, true);
        //CCLog("bullet countttt = %d",this->retainCount());
        delete this;
        return;

       
    }
    
    if (getPositionX() < 0 || getPositionX() > 320 || getPositionY() < 0 || getPositionY() > 480)
    {
        CCInteger *tag = CCInteger::create(getTag());
        CCDictionary *dict = CCDictionary::create();
        dict->setObject(tag, "tag");
        dict->setObject(CCInteger::create(ENEMYBULLET_DESTROY_OUT), "type");
        CCNotificationCenter::sharedNotificationCenter()->postNotification(DESTROY_BULLET_NOTIFICATION, dict);
    }
}